/*
 *  $Id: ReignOfFire.cs 1026 2010-05-14 08:46:06Z thenn.erannor $
 *	This file is a part of Ragima CCG.
 *	(C) 2008-2009 Ragima development group.
 *
 *	Licensed under the terms of the GNU GPL License version 2.
 *
 */

using System.Collections.Generic;

namespace Ragima.Cards001.Fire.Abilities {
    public class ReignOfFire: AbilityCard {
        public ReignOfFire(): base("001_f_a_reign_of_fire") {
            Level = CompleteLevel.ReadyForTest;
            Version = 0;
            Element = ElementType.Fire;
        }

        public override void DoAction(TargetArgs args) {
            DoReign(args.Player, args.Player);
            DoReign(args.Player, args.Player.Enemy);
            args.Caster.DamageDirectBy(NewDamage(args, DamageType.Regeneration, 2), true);
        }

        private static void DoReign(Player owner, Player target) {
            foreach (Land land in target.GetLandsWithoutAntimagic())
                DoReign(owner, land);
        }
        private static void DoReign(Player owner, Land target) {
            bool hasMana = false;
            foreach (ElementType element in ElementType.ManaElements) {
                if (element != ElementType.Fire && target.Summary.GetManaIncome(element) > 0) {
                    hasMana = true;
                    break;
                }
            }
            if (hasMana)
                target.AddAffect(new ReignOfFireAffect(owner));
        }

        private class ReignOfFireAffect: SpellAffect<ReignOfFire> {
            private Player _receiver;
            private List<ManaValue> _values;
            private int _mana;

            public ReignOfFireAffect(Player receiver): base(true) {
                DefaultDescription = true;
                Add(new TimeAffect(5));
                _receiver = receiver;
                _values = new List<ManaValue>();
                _mana = 0;
            }

            public override bool Dispellable {
                get { return false; }
            }

            public override string DescriptionText {
                get { return Description.GetText(_receiver, _mana); }
            }

            public override Affect Clone() {
                return new ReignOfFireAffect(_receiver);
            }

            public override void SetAffect() {
                base.SetAffect();
                _values.Clear();
                _mana = 0;
                foreach (ElementType element in ElementType.ManaElements) {
                    int income = LocalSummary.GetManaIncome(element);
                    if (income > 0) {
                        _values.Add(new ManaValue(element, income));
                        LocalSummary.ChangeManaOutcome(element, income);
                        GlobalSummary.ChangeManaOutcome(element, income);
                        _receiver.GlobalSummary.ChangeManaIncome(ElementType.Fire, income);
                        _mana += income;
                    }
                }
            }

            public override void ResetAffect() {
                base.ResetAffect();
                foreach (ManaValue mana in _values) {
                    LocalSummary.ChangeManaOutcome(mana.Element, -mana.Value);
                    GlobalSummary.ChangeManaOutcome(mana.Element, -mana.Value);
                    _receiver.GlobalSummary.ChangeManaIncome(ElementType.Fire, -mana.Value);
                }
                _values.Clear();
                _mana = 0;
            }
        }

        private class ManaValue {
            public readonly ElementType Element;
            public readonly int Value;

            public ManaValue(ElementType element, int value) {
                Element = element;
                Value = value;
            }
        }
    }
}
